package com.example.springboot.mapper;

//存放mybatis的sql语句

import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Mapper
public interface UserMapper {

    //    添加用户信息SQL
    @Insert("insert into user(username,password,name,phone,email,address,avatar,role) " +
            "values(#{username},#{password},#{name},#{phone},#{email},#{address},#{avatar},#{role})")
    void insert(User user);

    //    修改用户信息SQL
    @Update("update user set username = #{username}, password = #{password}, name = #{name}, phone = #{phone}," +
            " email = #{email}, address = #{address}, avatar = #{avatar} where id = #{id}")
    void update(User user);

    //    删除用户信息SQL
    @Delete("delete from user where id = #{id}")
    void deleteUser(Integer id);

    // 批量删除用户信息
    @Delete("<script>" +
            "delete from user where id in " +
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void batchdeleteUser(@Param("list") List<Integer> ids);

//    查询所有用户信息
    @Select("select * from user order by id desc")
    List<User> selectAll();

//    按id查询用户
    @Select("select * from user where id = #{id} order by id desc")
    User selectById(Integer id);

//    按用户名查询用户
    @Select("select * from user where username = #{username}")
    User selectByUsername(String username);

    //    封禁用户
    @Update("update user set banned = 1 where username = #{username}")
    void banned(String username);

    //    解禁用户
    @Update("update user set banned = 0 where username = #{username}")
    void unbanned(String username);
}
